package com.tjetc.controller;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.tjetc.common.Result;
import com.tjetc.domain.Dclasses;
import com.tjetc.domain.DorClasses;
import com.tjetc.domain.Nclasses;
import com.tjetc.domain.NurClasses;
import com.tjetc.service.DclassesService;
import com.tjetc.service.NclassesService;
import com.tjetc.service.impl.DclassesServiceImpl;
import com.tjetc.service.impl.NclassesServiceImpl;
import com.tjetc.utils.PageInfo;

import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.Method;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

@WebServlet("/classes/*")
@MultipartConfig
public class ClassesServlet extends HttpServlet {
    private DclassesService dclassesService = new DclassesServiceImpl();
    private NclassesService nclassesService = new NclassesServiceImpl();
    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String uri = request.getRequestURI();
        String methodName = uri.substring(uri.lastIndexOf("/") + 1);
        try {
            Method method = this.getClass().getDeclaredMethod(methodName, HttpServletRequest.class, HttpServletResponse.class);
            method.invoke(this,request,response);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    protected void saveDoc(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Integer did = Integer.valueOf(request.getParameter("id"));
        String days = request.getParameter("days");
        String[] split = days.split(",");
        for (String day : split) {
            Dclasses dclasses = new Dclasses();
            dclasses.setDid(did);
            dclasses.setDate(day);
            dclassesService.addDoc(dclasses);
        }
        new ObjectMapper().writeValue(response.getOutputStream(), Result.success("排班成功"));
    }

    protected void saveNur(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Integer nid = Integer.valueOf(request.getParameter("id"));
        String days = request.getParameter("days");
        String[] split = days.split(",");
        for (String day : split) {
            Nclasses nclasses = new Nclasses();
            nclasses.setNid(nid);
            nclasses.setDate(day);
            nclassesService.addNur(nclasses);
        }
        new ObjectMapper().writeValue(response.getOutputStream(), Result.success("排班成功"));
    }

    protected void getDocClassCount(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Integer did = Integer.valueOf(request.getParameter("did"));
        int count = dclassesService.getDocCount(did);
        new ObjectMapper().writeValue(response.getOutputStream(),count);
    }

    protected void getNurClassCount(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Integer nid = Integer.valueOf(request.getParameter("nid"));
        int count = nclassesService.getNurCount(nid);
        new ObjectMapper().writeValue(response.getOutputStream(),count);
    }

    protected void listDorClasses(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String curPage = request.getParameter("curPage");
        String ps = request.getParameter("pageSize");
        //接受请求参数
        //空处理:赋值给当前页码
        int currentPage = curPage == null ? 1:Integer.valueOf(curPage);
        //每页显示的条数
        int pageSize = ps == null ? 5 : Integer.parseInt(ps);
        //总记录数
        int count = dclassesService.getCount();
        PageInfo<DorClasses> pageInfo = new PageInfo<>(currentPage, count, pageSize);
        List<DorClasses> list = dclassesService.listDorClasses(pageInfo);
        pageInfo.setList(list);
        new ObjectMapper().writeValue(response.getOutputStream(),pageInfo);
    }

    protected void listNurClasses(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String curPage = request.getParameter("curPage");
        String ps = request.getParameter("pageSize");
        //接受请求参数
        //空处理:赋值给当前页码
        int currentPage = curPage == null ? 1:Integer.valueOf(curPage);
        //每页显示的条数
        int pageSize = ps == null ? 5 : Integer.parseInt(ps);
        //总记录数
        int count = nclassesService.getCount();
        PageInfo<NurClasses> pageInfo = new PageInfo<>(currentPage, count, pageSize);
        List<NurClasses> list = nclassesService.listNurClasses(pageInfo);
        pageInfo.setList(list);
        new ObjectMapper().writeValue(response.getOutputStream(),pageInfo);
    }

    protected void clearClasses(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 创建一个Calendar实例
        Calendar calendar = Calendar.getInstance();

        // 设置为当前日期所在周的周六晚十点的时间
        calendar.set(Calendar.DAY_OF_WEEK, Calendar.SATURDAY);
        calendar.set(Calendar.HOUR_OF_DAY,22);
        calendar.set(Calendar.MINUTE,0);
        calendar.set(Calendar.SECOND,0);
        calendar.set(Calendar.MILLISECOND,0);
        // 获取周六的时间
        Date sunday = calendar.getTime();
        //获取当前时间
        Date date = new Date();
        if (date.compareTo(sunday) < 0) {//说明当前的时间在本周六晚十点前，不允许清空排班表;注意一周的周日在周六之前
            new ObjectMapper().writeValue(response.getOutputStream(),Result.fail("请于本周六晚十点之后清空排班表"));
        }else{//也就是说只有两个小时来进行清空排班的时间段
            boolean b = dclassesService.clearClasses();
            if (b){//成功
                new ObjectMapper().writeValue(response.getOutputStream(),Result.success("成功清空，请及时安排下周排班"));
            }else{//失败
                new ObjectMapper().writeValue(response.getOutputStream(),Result.fail("系统错误，请检查"));
            }
        }
    }
}
